<?php
// 本类由系统自动生成，仅供测试用途
class SortAction extends BaseAction{
    public function index(){

		$id = (int)$_REQUEST['id'];

     	$dao = D("Sort");
     	$volist = $dao->order('id asc')->findAll();

     	$this->assign("list",$volist);
     	$this->assign('id',$id);

        $this->display();
    }


    public function sel_sort(){

		$id = (int)$_REQUEST['id'];

     	$dao = D("Sort");
     	$volist = $dao->where("id<>{$id} and pid<>{$id}")->order('id asc')->findAll();

		$s = '';
		for($i=0;$i<count($volist);$i++)
		{
        	if( !empty($s) ) $s .= ',';
        	$id = $volist[$i]['id'];
        	$pid = $volist[$i]['pid'];
        	$name = $volist[$i]['name'];
        	$s .= "{";
        	$s .= "id:{$id}";
        	$s .= ",pid:{$pid}";
        	$s .= ",name:'{$name}'";
        	$s .= "}";
		}
		$s = '['.$s.']';

		echo $s;

    }


    function moveTo()
    {
     	$id = (int)$_REQUEST['id'];
     	$pid = (int)$_REQUEST['pid'];
      	if( empty($id) )
      		$this->error('不能移动根栏目！ ');
        if( $id == $pid )
        	$this->error('非法操作，id=pid');

  		$dao = D('Sort');
  		$rt = $dao->where('id='.$id)->setField('pid',$pid);
  		if( $rt )
  		{
  			$this->_refresh_left($id);
  			$this->assign ( 'jumpUrl',__URL__.'/edit/id/'.$id );
  		 	$this->success('操作成功！');
  		}
  		else
  		{
        	$this->error('操作失败！');
  		}




    }

    function _before_AddOrEdit()
    {
     	$dao = D("Tpl");
     	$list_tpl = $dao->order('is_default desc')->where("tpl_type='list' and model='Artc'")->findAll();
     	$content_tpl = $dao->order('is_default desc')->where("tpl_type='content' and model='Artc'")->findAll();

     	//模板列表
     	$this->assign('list_tpl',$list_tpl);
     	$this->assign('content_tpl',$content_tpl);
     	
     	//得到表单列表
    	$m_form = D("Form");
    	$this->assign("form_list",$m_form->findAll());
    }


    public function _before_add()
    {
    	$pid = (int)$_GET['pid'];
		$this->_before_AddOrEdit();
    	$this->assign('pid',$pid);
    	
    }

    public function _before_edit()
    {
     	$this->_before_AddOrEdit();
     	$this->assign('img_dir',C('ARTC_IMG_DIR').'s_');
     	
     	
     	
    }

    public function _refresh_left($id)
    {
    	echo "<script language=\"javascript\">parent.left.window.location.href='".__URL__."/index/id/{$id}';</script>";

    }

    function _before_update()
    {
        $_POST['name'] = str_replace('\'','\'',$_POST['name'] );
        $_POST['name'] = str_replace('\\','\\\\',$_POST['name'] );
    	$id = (int)$_POST['id'];
    	$this->_refresh_left($id);
        Cookie::set ( '_currentUrl_', __URL__.'/edit/id/'.$id);
    }

    function insert()
    {
    	$model = D ('Sort');
    	$_POST['name'] = str_replace('\'','‘',$_POST['name'] );
    	$_POST['name'] = str_replace('\\','\\\\',$_POST['name'] );

        echo 0;
    	//处理上传的文章形象图片
		if( !empty( $_FILES['image']['size'] ) )
		{
            echo 1;
			$img = $this->_upload();

			if( $img != null )
			{
				echo 2;
        		$_POST['img'] = $img;
        	}
        }


		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		//保存当前数据对象
		$id = $list=$model->add ();
		if ($list!==false) { //保存成功
			$this->_refresh_left($id);
			$this->assign ( 'jumpUrl',__URL__.'/edit/id/'.$id );
			$this->success ('新增成功!');
		} else {
			//失败提示
			$this->error ('新增失败!');

		}
    }


    function update() {


		$model = D ( 'Sort' );

		//处理上传的文章形象图片
		if( !empty( $_FILES['image']['size'] ) )
		{

			$img = $this->_upload();

			if( $img != null )
			{
        		$_POST['img'] = $img;
        	}
        }

        //得到栏目信息
   		$id = (int)$_POST['id'];
   		$vo = $model->where('id='.$id)->find();

        // 更新数据
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		$list=$model->save ();
		if (false !== $list) {

			if( $img != null )
			{
				//删除栏目原来的图片
				$this->_del_img( $vo['img'] );
			}

			//成功提示
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success ('编辑成功!');
		} else {
			//错误提示
			$this->error ('编辑失败!');
		}
	}


    function del()
    {
    	$id = (int)$_REQUEST['id'];
    	$dao = D("Sort");

    	//检测栏目是否存在
    	$row = $dao->where('id='.$id)->find();
    	if( empty($row) )
    		$this->error('栏目不存在！');
		$pid = $row['pid'];

     	//检测是否存在子栏目
     	$cnt = $dao->where('pid='.$id)->count();
     	if( $cnt > 0 )	//存在子栏目
     	{
			$this->error('请先删除栏目下的子栏目！');
     	}

        //删除文章
        $dao_artc = D("Artc");

        $artc_list = $dao_artc->where('pid='.$id)->field('img')->select();
        $cnt = count( $artc_list );
        $rt = $dao_artc->where('pid='.$id)->delete();
        //删除文章图片
        for($i=0;$i<$cnt;$i++)
        {
         	$this->_del_img( $artc_list[$i]['img'] );
        }
        unset($artc_list);


		//得到栏目信息
		$sort = $dao->where('id='.$id)->find();

        //删除栏目
        if( $dao->where('id='.$id)->delete() )
        {
            //删除栏目图片
            $this->_del_img( $sort['img'] );

        	if( $pid == 0 )
        	{
        		$this->_refresh_left(0);
        		$this->assign ( 'jumpUrl',__URL__.'/add/id/'.$pid );
        	}
        	else
        	{
        		$this->_refresh_left($pid);
        		$this->assign ( 'jumpUrl',__URL__.'/edit/id/'.$pid );
        	}
        	$this->success ('删除成功！' );
        }
		else
		{
			$this->error ('删除失败！');
		}

    }

    protected function _upload()
    {
        import("ORG.Net.UploadFile");
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize  = 1024*500 ;
        //设置上传文件类型
        $upload->allowExts  = explode(',','jpg,gif,png,jpeg');
        //设置附件上传目录
        $upload->savePath =  C('ARTC_IMG_UPLOAD_DIR');
	    //设置需要生成缩略图，仅对图像文件有效
       $upload->thumb =  true;
       //设置需要生成缩略图的文件后缀
	    $upload->thumbPrefix   =  's_';  //生产2张缩略图
       //设置缩略图最大宽度
		$upload->thumbMaxWidth =  C('THUMB_W');
       //设置缩略图最大高度
		$upload->thumbMaxHeight = C('THUMB_H');
	   //设置上传文件规则
	   $upload->saveRule = uniqid;
	   //删除原图
	   //$upload->thumbRemoveOrigin = true;

        if(!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
            return null;
        }else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            //$_POST['image']  = $uploadList[0]['savename'];
            return $uploadList[0]['savename'];
        }
	}

	//删除文章图片
	private function _del_img($img)
	{
     	if( empty($img) )  	//如果没有图片,则直接返回
     		return true;

      	$img_path =  C('ARTC_IMG_UPLOAD_DIR');
       	unlink(  $img_path.$img );	//删除原图
       	unlink(  $img_path.'s_'.$img );	//删除缩略图

       	return true;

	}




}
?>